﻿#include "DATypes.h"
#include "DAFuncTools.h"
#include "qscreen.h"
#include <QIcon>
#include <QStyle>
#include <QApplication>
#ifdef _WIN32
#include <tchar.h>
#include <Windows.h>
#include <Netlistmgr.h>
#else
#include <netdb.h>
#include <arpa/inet.h>
#endif

void DAFunTools::init()
{

}

void DAFunTools::ToCenter(QWidget *parents)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
    QScreen *ScreenObj = parents->screen();
    QRect RectObj = ScreenObj->geometry();
    int win_wid = RectObj.width() - parents->width();
    int win_hig = RectObj.height() - parents->height();
    parents->move( win_wid  / 2, win_hig / 2.5);

#else
    parents->move (
                (QApplication::desktop()->width() - parents->width())/2,
                (QApplication::desktop()->height() - parents->height())/2
                );
#endif

}

void DAFunTools::ToRight(QWidget *parents)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
    QScreen *ScreenObj = parents->screen();
    parents->setGeometry(QStyle::alignedRect(
                        Qt::RightToLeft,
                        Qt::AlignVertical_Mask,
                        parents->size(),
                        ScreenObj->availableGeometry())
                         );
#else
    parents->move (
                (QApplication::desktop()->width() - parents->width()),
                (QApplication::desktop()->height() - parents->height())/2
                );
#endif
}
void DAFunTools::ToLeft(QWidget *parents)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
    QScreen *ScreenObj = parents->screen();
    parents->setGeometry(QStyle::alignedRect(
                        Qt::LeftToRight,
                        Qt::AlignVertical_Mask,
                        parents->size(),
                        ScreenObj->availableGeometry())
                         );
#else
    parents->move (
                (QApplication::desktop()->width() - parents->width()),
                (QApplication::desktop()->height()/2 - parents->height())
                );
#endif
}

QIcon DAFunTools::StdIcon(int num)
{
    QStyle* style = QApplication::style();
    QIcon icon = style->standardIcon((QStyle::StandardPixmap)num);
//    QPixmap m_pic = icon.pixmap(icon.actualSize(QSize(LIco->width()-9,LIco->height()-5)));//size自行调整
    return icon;
}

bool DAFunTools::isNetOnline()
{
//     //检测操作系统是否能连通外部链接
    bool flag = false;
    char ip[100] = {0};
    QVector<const char *> t_Hostname;
    t_Hostname.push_back("www.baidu.com");
    t_Hostname.push_back("www.163.com");
    t_Hostname.push_back("www.qq.com");
    for (int i = 0;i < t_Hostname.size();++i)
    {
        struct hostent *he;
        struct in_addr **addr_list;
        if ((he = gethostbyname(t_Hostname[i])) == nullptr)
        {
            flag = false;
        }
        else
        {
            addr_list = (struct in_addr **)he->h_addr_list;
            for (i = 0; addr_list[i] != nullptr; i++)
            {
#ifdef Q_OS_WINDOWS
                strcpy_s(ip, inet_ntoa(*addr_list[i]));
#else
                strcpy(ip, inet_ntoa(*addr_list[i]));
#endif
                flag = true;
                break;
            }
            break;
        }
    }
    return flag;
}

QString DAFunTools::WeekE2C(QString English)
{
    QString week;
    if(English == QString("Monday"))
    {
        return "星期一";
    }
    if(English == QString("Tuesday"))
    {
        return "星期二";
    }
    if(English == QString("Wednesday"))
    {
        return "星期三";
    }
    if(English == QString("Thursday"))
    {
        return "星期四";
    }
    if(English == QString("Friday"))
    {
        return "星期五";
    }
    if(English == QString("Saturday"))
    {
        return "星期六";
    }
    if(English == QString("Sunday"))
    {
        return "星期日";
    }
    return English;
}

void DAFunTools::SetPosition(QWidget *parents,POSITION position)
{
    switch (position) 
    {
        case POSITION::Left:
            ToLeft(parents);
            break;
        case POSITION::Center:
            ToCenter(parents);
            break;
        case POSITION::Right:
            ToRight(parents);
            break;
        default:
            break;
    }
    return;
}
